package ch.unisi.inf.gui.action;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import ch.unisi.inf.engine.Image;

import com.jscape.inet.mime.MimeException;
import com.jscape.inet.smtp.SmtpException;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


/**
 * 
 * @author Mohamed.Mosa@lu.unisi.ch
 * 
 * This is the GUI that will pop up when the user clicks on 
 * the mail button in the Main frame window. This GUI consists 
 * of 8 fields for the user to enter. the only one not pre-
 * filled is the password. And the password is not visible.
 */

public final class MailGui extends JDialog {

	private final static int TEXT_FIELD_WIDTH = 25;

	private final static int GRID_X_ZERO = 0;
	private final static int GRID_X_ONE = 1;

	private final static int GRID_Y_ZERO = 0;
	private final static int GRID_Y_ONE = 1;
	private final static int GRID_Y_TWO = 2;
	private final static int GRID_Y_THREE = 3;
	private final static int GRID_Y_FOUR = 4;
	private final static int GRID_Y_FIVE = 5;
	private final static int GRID_Y_SIX = 6;
	private final static int GRID_Y_SEVEN = 7;

	private final static int TOP_BORDER = 5;
	private final static int LEFT_BORDER = 5;
	private final static int BOTTOM_BORDER = 5;
	private final static int RIGHT_BORDER = 5;

	private final static int BUTTON_WIDTH = 150;

	private final static int BUTTON_TOP_INSET = -100;//0
	private final static int BUTTON_LEFT_INSET = 10;
	private final static int BUTTON_BOTTOM_INSET = 0;//0
	private final static int BUTTON_RIGHT_INSET = 10;//0

	//Labels 
	private final JLabel hostLabel;
	private final JLabel loginLabel;
	private final JLabel passLabel;
	private final JLabel toLabel;
	private final JLabel fromLabel;
	private final JLabel subjectLabel;
	private final JLabel bodyLabel;
	private final JLabel imageLabel;  

	//Text Fields
	private final JTextField host;
	private final JTextField login;
	private final JTextField to;
	private final JTextField from;
	private final JTextField subject;
	private final JTextField body;
	private final JTextField imagePath; 

	private final JPasswordField pass;
	//Message Sent Window
	private JDialog confirm;
	//My buttons
	private final JButton sendButton;
	private final JButton cancelButton;
	//Layout
	private final GridBagLayout gridBagConstraints;
	//Panels
	private final JPanel top;

	private final Image image;

	public MailGui(final Image image){//final MainFrame parent){
		this.image = image;
		this.setTitle("Send Mail");
		this.setSize(new Dimension(1000,500));
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

		top = new JPanel (gridBagConstraints = new GridBagLayout());
		top.setBorder(BorderFactory.createEmptyBorder(TOP_BORDER, LEFT_BORDER, BOTTOM_BORDER, RIGHT_BORDER));
		GridBagConstraints gbc = new GridBagConstraints();
		setLayout(new GridLayout(2,1));//r,c

		//alignment center
		gbc.anchor = GridBagConstraints.CENTER; 

		//label for Host
		hostLabel = new JLabel("Host Name: ");
		gbc.gridx = GRID_X_ZERO;
		gbc.gridy = GRID_Y_ZERO;
		top.add(hostLabel, gbc);

		//label for Login
		loginLabel = new JLabel("User Name: ");
		gbc.gridx = GRID_X_ZERO;
		gbc.gridy = GRID_Y_ONE;
		top.add(loginLabel, gbc);

		//label for Password
		passLabel = new JLabel("Password: ");
		gbc.gridx = GRID_X_ZERO;
		gbc.gridy = GRID_Y_TWO;
		top.add(passLabel, gbc);

		//label for to receipent
		toLabel = new JLabel("Mail To: ");
		gbc.gridx = GRID_X_ZERO;
		gbc.gridy = GRID_Y_THREE;
		top.add(toLabel, gbc);

		//label for from recepiant
		fromLabel = new JLabel("Mail From: ");
		gbc.gridx = GRID_X_ZERO;
		gbc.gridy = GRID_Y_FOUR;
		top.add(fromLabel, gbc);

		//label for subject
		subjectLabel = new JLabel("Subject: ");
		gbc.gridx = GRID_X_ZERO;
		gbc.gridy = GRID_Y_FIVE;
		top.add(subjectLabel, gbc);

		//label for Body
		bodyLabel = new JLabel("Body: ");
		gbc.gridx = GRID_X_ZERO;
		gbc.gridy = GRID_Y_SIX;
		top.add(bodyLabel, gbc);

		//label for attachment 
		imageLabel = new JLabel("Attachments: ");
		gbc.gridx = GRID_X_ZERO;
		gbc.gridy = GRID_Y_SEVEN;
		top.add(imageLabel, gbc);


		//***************************************
		//text field for Host
		host = new JTextField("mail.usilu.net", TEXT_FIELD_WIDTH); 
		gbc.gridx = GRID_X_ONE;
		gbc.gridy =GRID_Y_ZERO;
		top.add(host, gbc);

		//text field for login
		login = new JTextField("mosam", TEXT_FIELD_WIDTH);
		gbc.gridx = GRID_X_ONE;
		gbc.gridy = GRID_Y_ONE;
		top.add(login, gbc);

		//text field for Password
		pass = new JPasswordField(TEXT_FIELD_WIDTH); 
		gbc.gridx = GRID_X_ONE;
		gbc.gridy =GRID_Y_TWO;
		top.add(pass, gbc);

		//text field for to reciepant
		to = new JTextField("m_h_m1@msn.com",TEXT_FIELD_WIDTH); 
		gbc.gridx = GRID_X_ONE;
		gbc.gridy =GRID_Y_THREE;
		top.add(to, gbc);

		//text field for from recepiant
		from = new JTextField("mosam@lu.unisi.ch",TEXT_FIELD_WIDTH); 
		gbc.gridx = GRID_X_ONE;
		gbc.gridy =GRID_Y_FOUR;
		top.add(from, gbc);

		//text field for Subject
		subject = new JTextField("Photo Manager Attachment",TEXT_FIELD_WIDTH); 
		gbc.gridx = GRID_X_ONE;
		gbc.gridy =GRID_Y_FIVE;
		top.add(subject, gbc);

		//text field for Body
		body = new JTextField("A photo has been been attached",TEXT_FIELD_WIDTH); 
		gbc.gridx = GRID_X_ONE;
		gbc.gridy =GRID_Y_SIX;
		top.add(body, gbc);

		//text field for Attachments
		imagePath = new JTextField(TEXT_FIELD_WIDTH); 
		imagePath.setText(image.getPath());
		gbc.gridx = GRID_X_ONE;
		gbc.gridy =GRID_Y_SEVEN;
		top.add(imagePath, gbc);


		//************************************
		//send button 
		sendButton = new JButton ("Send");
		Dimension size = sendButton.getPreferredSize ();
		size.width = BUTTON_WIDTH;
		sendButton.setPreferredSize (size);


		//send button listener
		sendButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				System.out.println("Send email button was clicked");
				System.out.println("Inside the MailGui in the send listener    "+ image.getPath());
				EmailSender mail = new EmailSender();

				try {
					mail.send(host.getText(), to.getText(),
							from.getText(), subject.getText(), 
							body.getText(), imagePath.getText(),
							login.getText(), pass.getText());

				} catch (SmtpException e) {
					e.printStackTrace();
					e.getMessage();
					confirm = new JDialog();
					JOptionPane.showMessageDialog(confirm, e.getMessage());
					return;

				} catch (IOException e) {
					e.printStackTrace();
					e.getMessage();
					confirm = new JDialog();
					JOptionPane.showMessageDialog(confirm, e.getMessage());
					return;

				} catch (MimeException e) {
					e.printStackTrace();
					e.getMessage();
					confirm = new JDialog();
					JOptionPane.showMessageDialog(confirm, e.getMessage());
					return;

				} catch (Exception e) {
					confirm = new JDialog();
					JOptionPane.showMessageDialog(confirm, "An unknown error has occured");
					return;
				}
				setVisible(false);

				//Confirmation Window
				confirm = new JDialog();
				JOptionPane.showMessageDialog(confirm, "Your Message Has Been Sent. ");
			}
		}
		);

		//cancel button
		cancelButton = new JButton ("Cancel");
		size = cancelButton.getPreferredSize ();
		size.width = BUTTON_WIDTH;
		cancelButton.setPreferredSize (size);

		//cancel button listener
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				setVisible(false);
			}
		}
		);

		//button panel
		final JPanel buttonPanel = new JPanel (new GridBagLayout ());
		final GridBagConstraints buttonGridBagConstraints = new GridBagConstraints ();
		buttonGridBagConstraints.insets = new Insets (BUTTON_TOP_INSET, BUTTON_LEFT_INSET, BUTTON_BOTTOM_INSET, BUTTON_RIGHT_INSET);
		buttonGridBagConstraints.fill = GridBagConstraints.SOUTHEAST;
		buttonPanel.add (sendButton, buttonGridBagConstraints);
		buttonPanel.add (cancelButton, buttonGridBagConstraints);

		this.add(top);
		this.add(buttonPanel);
		this.setLocationByPlatform(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		this.pack();
		this.setVisible(true);
	}

}
